import baimaLibs from "./libs";

const model = {
  version: "1.0.0",
  mixin(key, defaultCode, list = []) {
    let extraLibs = [];
    for (let i = 0; i < list.length; i++) {
      let libKey = list[i];
      let lib = baimaLibs[libKey];
      if (lib) {
        let libs = lib.libs || {};
        for (let key in libs) {
          let item = libs[key];
          let d = item.d || [];
          extraLibs.push(...d);
        }
      }
    }
    extraLibs = Array.from(new Set(extraLibs));
    model[key] = {
      key,
      defaultCode,
      libs: list,
      extraLibs,
    };
  },
  extend(parentKey, key, { libs, defaultCode }) {
    let targetKey = `${parentKey}/${key}`;
    let parent = model[parentKey];
    let target = { ...parent };
    if (libs) {
      target.libs = [...target.libs, ...libs];
      target.libs = Array.from(new Set(target.libs));
    }

    if (defaultCode) {
      target.defaultCode = defaultCode;
    }
    model.mixin(targetKey, target.defaultCode, target.libs);
  },
};

//流程>步骤>编程
model.mixin(
  `flow/procedure/code`,
  `async function runProcess($model = model,$plugin = plugin,$params){

}`,
  ["common", "flow"]
);

//自定义页面>功能
model.mixin(
  `display/action`,
  `function runDisplayAction(key,value,$view = view){
}`,
  ["common", "display"]
);

//api>云函数
model.mixin(
  `api/function`,
  `async function run($input,$output,$modules = modules){
}`,
  ["common", "api"]
);

//api>云函数>企业微信
model.extend(`api/function`, `wxwork`, {
  libs: ["wxwork"],
});

//api>云函数>钉钉
model.extend(`api/function`, `dingtalk`, {
  libs: ["dingtalk"],
});

//api>云函数>微信公众号
model.extend(`api/function`, `weixinmp`, {
  libs: ["weixinmp"],
});

//api>webhook
model.mixin(
  `api/webhook`,
  `async function hook($req = request,$resp = response,$modules = modules){
}`,
  ["common", "api", "egg"]
);

//api>webhook>企业微信
model.extend(`api/webhook`, `wxwork`, {
  libs: ["wxwork"],
});

//api>webhook>钉钉
model.extend(`api/webhook`, `dingtalk`, {
  libs: ["dingtalk"],
});

export default model;
